package com.ruoyi.system.controller.trade;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.common.BillTypeConstant;
import com.ruoyi.system.domain.db.financial.FinancialEconomic;
import com.ruoyi.system.domain.db.trade.TradeCargoOrder;
import com.ruoyi.system.domain.vo.OrderWarehouseStateVo;
import com.ruoyi.system.domain.vo.PageInfo;
import com.ruoyi.system.domain.vo.TradeCargoOrderVo;
import com.ruoyi.system.service.IFinancialEconomicService;
import com.ruoyi.system.service.ITradeCargoOrderService;
import com.ruoyi.system.utils.MyPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.core.page.TableSupport.PAGE_NUM;
import static com.ruoyi.common.core.page.TableSupport.PAGE_SIZE;

/*
    * 订单管理
 */
@RestController
@RequestMapping("/trade/cargo")
@Slf4j
public class TradeCargoOrderController extends BaseController {

    @Autowired
    private ITradeCargoOrderService tradeCargoOrderService;

    @Autowired
    private IFinancialEconomicService financialEconomicService;

    /**
     * 查询订单管理列表
     */

    @GetMapping("/list")
    public PageInfo<TradeCargoOrderVo> list(TradeCargoOrder tradeCargoOrder)
    {
       // startPage();
        List<TradeCargoOrderVo> list = tradeCargoOrderService.selectTradeCargoOrderList(tradeCargoOrder);
        return MyPageUtils.startPage(list);
    }

    /**
     * 获取订单详细信息
     */

    @GetMapping("")
    public AjaxResult getInfo(@RequestParam(required = false) Long orderId)
    {
        return success(tradeCargoOrderService.selectCargoOrderByOrderId(orderId));
    }

    /**
     * 新增订单管理
     */

    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@RequestBody TradeCargoOrder tradeCargoOrder)
    {
        int result = tradeCargoOrderService.insertTradeCargoOrder(tradeCargoOrder);
        FinancialEconomic financialEconomic = new FinancialEconomic(
                BillTypeConstant.CARGO_ORDER,tradeCargoOrder.getOrderId(),
                tradeCargoOrder.getPrice(),tradeCargoOrder.getRemark() // 备注从remark改过去
        );
        financialEconomicService.insertFinancialEconomic(financialEconomic);
        return toAjax(result);
    }

    /**
     * 修改订单管理
     */

//    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody TradeCargoOrder tradeCargoOrder)
//    {
//        boolean flag = false;
//        int result = tradeCargoOrderService.updateTradeCargoOrder(tradeCargoOrder);
//        FinancialEconomic financialEconomic = financialEconomicService.getBillByOrderIdAndBillTypeId(tradeCargoOrder.getOrderId(), BillTypeConstant.CARGO_ORDER);
//        if (!financialEconomic.getSalary().equals(tradeCargoOrder.getPrice())) {
//            financialEconomic.setSalary(tradeCargoOrder.getPrice());
//            flag = true;
//        }
//        if (financialEconomic.getInfo() == null) {
//            financialEconomic.setInfo("");
//        }
//        if (!financialEconomic.getInfo().equals(tradeCargoOrder.getRemark())) {
//            financialEconomic.setInfo(tradeCargoOrder.getRemark());
//            flag = true;
//        }
//        if (flag) { // 变了就改，不然不改
//            financialEconomicService.updateFinancialEconomic(financialEconomic);
//        }
//        return toAjax(result);
//    }

    /**
     * 删除订单管理
     */

    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("")
    public AjaxResult remove(@RequestBody List<Long> orderIds)
    {
        List<Long> billIds = new ArrayList<>();
        for (Long orderId : orderIds) {
            Long billId = financialEconomicService.getBillByOrderIdAndBillTypeId(orderId, BillTypeConstant.CARGO_ORDER).getBillId();
            billIds.add(billId);
        }
        financialEconomicService.deleteFinancialEconomicByOrderIds(billIds);
        return toAjax(tradeCargoOrderService.deleteTradeCargoOrderByOrderIds(orderIds));
    }

    /**
     * 删除订单管理
     */

    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderId}")
    public AjaxResult removeById(@PathVariable Long orderId)
    {
        FinancialEconomic financialEconomic = financialEconomicService.getBillByOrderIdAndBillTypeId(orderId, BillTypeConstant.CARGO_ORDER);
        financialEconomicService.deleteFinancialEconomicByOrderId(financialEconomic.getBillId());
        return toAjax(tradeCargoOrderService.deleteTradeCargoOrderByOrderId(orderId));
    }

    /**
     * 获取货物的运输信息
     */

    @GetMapping("/getCargoInfo")
    public TableDataInfo getCargoInfo(@RequestParam Long orderId){
        List<OrderWarehouseStateVo> orderWarehouseStateVos = tradeCargoOrderService.searchList(orderId);
        return getDataTable(orderWarehouseStateVos);
    }
}
